package com.piz.web.product;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.piz.model.ItemDetails;
import com.piz.model.ItemPricing;
import com.piz.model.Serve;
import com.piz.service.common.UnexpectedServiceException;
import com.piz.service.productservice.ProductService;


@Controller
@RequestMapping(value = "addProduct")
public class AddProductController {
	
	@Autowired
	private ProductService productService;
	
	@Autowired
	private AddProductValidation addProductValidation;
	
	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView showItem(@RequestParam(value="id", required = false,defaultValue = "0.0") Long id,
			ItemDetails itemDetails, Model model, HttpServletRequest request)
	{
		Map<String,Object> attributes = new HashMap<String, Object>();
		if(id != null && id > 0.0)
		{
			itemDetails = productService.fetchPizzaById(id);
			attributes.put("edit", true);
		}
		if(itemDetails == null)
		{
			itemDetails = new ItemDetails();
		}
		List<Serve> serves = null;
		try {
			serves = productService.getAllProductServes();
		} catch (UnexpectedServiceException e) {
			e.printStackTrace();
		}
		if(itemDetails.getPrices() == null || itemDetails.getPrices().size()==0)
		{
			List<ItemPricing> itempricings = new ArrayList<ItemPricing>();
			
			for(Serve serve : serves)
			{
				ItemPricing itemprice =  new ItemPricing();
				itemprice.setServe(serve);
				itempricings.add(itemprice);
			}
			
			
			itemDetails.setPrices(itempricings);
		}
		
		if(itemDetails.getPrices().size() == 1)
		{
			itemDetails.setSinglePrice(itemDetails.getPrices().get(0).getPrice());
			itemDetails.getPrices().get(0).setPrice(null);
		}
		
		try {
			attributes.put("categories", productService.getProductTypes());
			attributes.put("productTypes", productService.getAllCategories());
			attributes.put("tastes", productService.getAllTastes());
		} catch (UnexpectedServiceException e) {
			e.printStackTrace();
		}
		
		attributes.put("serves", serves);
		model.addAttribute(itemDetails);
		model.addAllAttributes(attributes);
		Map<String, Object> refrenceData = new HashMap<String, Object>();
		return new ModelAndView("admin/addProduct","refrenceData",refrenceData);
		
	}
	
	@SuppressWarnings("deprecation")
	@RequestMapping(method = RequestMethod.POST)
	public String processProduct(HttpServletRequest request, @Valid  @ModelAttribute ItemDetails itemDetails,BindingResult result,Map<String, Object> model)
	{
		addProductValidation.validate(itemDetails, result);
		 
		if (result.hasErrors()) 
		{
			List<Serve> serves;
			try {
				serves = productService.getAllProductServes();
				model.put("categories", productService.getProductTypes());
				model.put("productTypes", productService.getAllCategories());
				model.put("tastes", productService.getAllTastes());
				model.put("serves", serves);
			} catch (UnexpectedServiceException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			model.put("itemDetails", itemDetails);
			
			return "admin/addProduct";
		}
		try {
			productService.addProduct(itemDetails);
			productService.saveProductImage(request.getRealPath(""), itemDetails);
		} catch (UnexpectedServiceException e) {
			e.printStackTrace();
		}
		
		return "redirect: productList.action";
	}

	public ProductService getProductService() {
		return productService;
	}

	public void setProductService(ProductService productService) {
		this.productService = productService;
	}

	public AddProductValidation getAddProductValidation() {
		return addProductValidation;
	}

	public void setAddProductValidation(AddProductValidation addProductValidation) {
		this.addProductValidation = addProductValidation;
	}

}
